package day07_Array;

import java.util.ArrayList;

//ArrayList["a","b","a","a","b","b","c","a","a","b","b","c","a","a","b","b","c","a","a","b","b","c",]
//求每个字母出现的次数
public class Demo09_ArrayList {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("a");
        list.add("a");
        list.add("c");
        list.add("a");
        list.add("d");
        list.add("a");
        list.add("a");
        ArrayList<String> letter = new ArrayList<>();//用于存储每个字母的（分类的结果）
        for (int i = 0; i <list.size() ; i++) {
            //letter.contains(list.get(i))表示list.get(i)是否在letter中
          if (!letter.contains(list.get(i))){
              //不存在就加入
              letter.add(list.get(i));
          }
        }
        System.out.println(letter);//[a,b,c,d]
        //匹配然后累加输出
        for (int i = 0; i <letter.size() ; i++) {//外循环循环分类
            int sum=0;//用于统计每个单词出现的次数（定义在分类之下）
            for (int j = 0; j <list.size() ; j++) {//内循环循环所有数据
                if (letter.get(i).equals(list.get(j))){
                    sum++;
                }
            }
            System.out.println(letter.get(i)+":"+sum);
        }
    }
}
